package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.exception.ErrorCode;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.modules.security.user.SecurityUser;
import io.aircraft.modules.security.user.UserDetail;
import io.aircraft.modules.wms.dto.BindDTO;
import io.aircraft.modules.wms.service.BindService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 绑盘信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-18
 */
@RestController
@RequestMapping("wms/bind")
public class BindController {
    @Autowired
    private BindService bindService;

    @GetMapping("page")
    @RequiresPermissions("wms:bind:page")
    public Result<PageData<BindDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<BindDTO> page = bindService.page(params);

        return new Result<PageData<BindDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:bind:info")
    public Result<BindDTO> get(@PathVariable("id") Long id) {
        BindDTO data = bindService.get(id);

        return new Result<BindDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:bind:save")
    public Result save(@RequestBody BindDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        bindService.save(dto);

        return new Result();
    }

    @PostMapping("delivery")
    @LogOperation("空框登记")
    @RequiresPermissions("wms:bind:save")
    public Result bind(@ApiIgnore @RequestBody Map<String, Object> params) {
        //调用绑盘逻辑
        ProcResult procResult = bindService.bind(params);
        //处理结果
        if (!"0".equals(procResult.getCode())) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("register")
    @LogOperation("组盘登记")
    public Result register(@ApiIgnore @RequestBody Map<String, Object> params) {
        ProcResult procResult = bindService.manualRegister(params);
        //处理结果
        if (!"0".equals(procResult.getCode())) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:bind:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        bindService.delete(ids);

        return new Result();
    }
}